package com.changgou.seckill.service.impl;

import com.changgou.common.pojo.SeckillStatus;
import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.dao.SeckillOrderMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.service.SeckillOrderService;
import com.changgou.seckill.task.MultiThreadingCreadingCreateorder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author tu zy
 * @version 1.0
 * @date 2021/7/21 21:11
 */

@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private MultiThreadingCreadingCreateorder multiThreadingCreadingCreateorder;





    /**
     * 秒杀下单--排队
     */
    @Override
    public Boolean addQueue(String time, Long id, String username) {
        //记录用户排队的次数
        //1：key
        //2.自增的值
        Long userQueueCount = redisTemplate.boundHashOps("UserQueue").increment(username, 1);

        if (userQueueCount>1){
            //100表示重复排队
            throw new RuntimeException("100");
        }


        //创建排队对象
        SeckillStatus seckillStatus = new SeckillStatus(username,new Date(),1,id,time);

        //用户抢单排队
        redisTemplate.boundListOps("UserQueue").leftPush(seckillStatus);
        //用户抢单状态->用于查询
        redisTemplate.boundHashOps("UserQueueStatus").put(username,seckillStatus);

        //异步执行
        multiThreadingCreadingCreateorder.createOrder();

        return true;
    }

    /**
     * 抢单状态查询
     * @param username
     * @return
     */
    @Override
    public SeckillStatus queryStatus(String username) {

        return (SeckillStatus)redisTemplate.boundHashOps("UserQueueStatus").get(username);
    }


    /**
     * 下单
     * @param time
     * @param id
     * @param username
     */
    @Override
    public Boolean add(String time, Long id, String username) {


        //异步执行   本来会等十秒钟  但是这里并不会
        multiThreadingCreadingCreateorder.createOrder();
        //查询秒杀商品
       String nameSpace = "SeckillGoods_"+time;
       SeckillGoods seckillGoods = (SeckillGoods)redisTemplate.boundHashOps(nameSpace).get(id);

       //判断有没有库存
        if (seckillGoods==null || seckillGoods.getStockCount()<=0){
            throw new RuntimeException("已售罄！");
        }


        //创建订单对象
        SeckillOrder seckillOrder = new SeckillOrder();

        //秒杀商品Id
        seckillOrder.setSeckillId(id);

        //支付金额
        seckillOrder.setMoney(seckillGoods.getCostPrice());

        //用户
        seckillOrder.setUserId(username);

        //创建时间
        seckillOrder.setCreateTime(new Date());

        //状态   0  --- 未支付
        seckillOrder.setStatus("0");


        //将订单对象存储起来
        //1. 一个用户只能有一个未支付秒杀订单
        //2.订单存入Redis
        //  Hash
        //      namespace   SeckillOrder
        //                      username:SeckillOrder

        redisTemplate.boundHashOps("SeckillOrder").put(username,seckillOrder);

        /**
         * 库存递减
         * Redis.stockCount
         * 商品有可能是最后一个，如果是最后一个，则将Redis中商品信息删除，并且将Redis中该商品的数据同步到MySQL
         *
         */
        seckillGoods.setStockCount(seckillGoods.getStockCount()-1);

        if (seckillGoods.getStockCount()<=0){
            //同步数据到MySQL
            seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);

            //删除Redis中的商品数据
            redisTemplate.boundHashOps(nameSpace).delete(id);
        }else{
            //同步数据到Redis
            redisTemplate.boundHashOps(nameSpace).put(id,seckillGoods);
        }

        return true;

    }


}
